package com.noble.meether.ui.reg.info;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.camera.ImgSelectUtil;
import com.noble.library.dir.DirType;
import com.noble.library.image.ImageLoader;
import com.noble.library.log.PToast;
import com.noble.library.third.ThirdConstant;
import com.noble.library.utils.BitmapUtil;
import com.noble.library.utils.FileUtil;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;
import com.noble.meether.logic.center.EditKey;
import com.noble.meether.logic.media.MediaConstant;
import com.noble.meether.logic.media.UpLoadResult;

import java.util.HashMap;

/**
 * 注册资料填写页
 */
public class RegInfoAct extends BaseAppCompatActivity implements ImgSelectUtil.OnChooseCompleteListener, View.OnClickListener {
    private ImageView user_head, reg_man, reg_female;
    private EditText etName;

    private String avatarLink = "";
    private String nickName = "";
    private int gender = -1;                    // 性别：不设置默认
    private boolean isThirdAvatar = false;      // 是否是三方头像

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reg_info_act);
        initView();
    }

    private void initView() {
        setBackView("个人信息");
        user_head = findViewById(R.id.user_head);
        reg_man = findViewById(R.id.reg_man);
        reg_female = findViewById(R.id.reg_female);
        etName = findViewById(R.id.reg_name);

        user_head.setOnClickListener(this);
        reg_man.setOnClickListener(this);
        reg_female.setOnClickListener(this);
        findViewById(R.id.login_btn).setOnClickListener(this);

        avatarLink = ThirdConstant.Third_Avatar;
        nickName = ThirdConstant.Third_NickName;
        etName.setText(nickName);
        if (!TextUtils.isEmpty(avatarLink)) {
            isThirdAvatar = true;
            ImageLoader.loadCircleAvatar(this, avatarLink, user_head);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_head:
                MeetUIShow.showImgSelectDialog(this, this);
                break;

            case R.id.reg_man:
                gender = 1;
                reg_man.setSelected(true);
                reg_female.setSelected(false);
                break;

            case R.id.reg_female:
                gender = 2;
                reg_man.setSelected(false);
                reg_female.setSelected(true);
                break;

            case R.id.login_btn:
                login();
                break;
        }
    }

    private void login() {
        String name = etName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            PToast.showShort(getString(R.string.toast_nickname_isnull));
            return;
        }
        if (gender == -1) {
            PToast.showShort("请选择性别");
            return;
        }

        if (gender == 2 && TextUtils.isEmpty(avatarLink)) {
            PToast.showShort("请选择头像上传");
            return;
        }

        LoadingDialog.show(this, getString(R.string.loading_reg));
        HashMap<String, Object> jsonMap = new HashMap<>();
        if (!TextUtils.isEmpty(avatarLink)) {
            jsonMap.put(EditKey.s_key_avatar, avatarLink);
        }
        jsonMap.put(EditKey.s_key_nickName, name);
        jsonMap.put(EditKey.s_key_gender, gender);
        jsonMap.put(EditKey.s_key_avatarStatus, isThirdAvatar ? 1 : -2);
        ModuleMgr.getCenterMgr().updateMyInfo(jsonMap, response -> {
            if (response.isOk()) {
                UIShow.showMainClearTask(RegInfoAct.this);
            } else {
                PToast.showShort("资料更新失败，请重试");
            }
        });
    }

    @Override
    public void onUrl(String url) {
        LoadingDialog.show(this, "正在上传头像");
        final String smallPath = BitmapUtil.getSmallBitmapAndSave(url, DirType.getUploadDir());
        if (FileUtil.isExist(smallPath)) {
            ModuleMgr.getMediaMgr().sendHttpFile(MediaConstant.UPLOAD_TYPE_AVATAR, smallPath, response -> {
                LoadingDialog.closeLoadingDialog();
                FileUtil.deleteFile(smallPath);

                PToast.showShort(response.getMsg());
                if (response.isOk()) {
                    UpLoadResult result = (UpLoadResult) response.getBaseData();
                    avatarLink = result.getUploadUrl();
                    isThirdAvatar = false;
                    ImageLoader.loadCircleAvatar(RegInfoAct.this, avatarLink, user_head);
                }
            });
        } else {
            LoadingDialog.closeLoadingDialog();
            PToast.showShort("图片地址无效");
        }
    }
}
